package com.xiaoxu.spark

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SQLContext;

/**
  * SQLContext：spark1.X中使用
  * http://spark.apache.org/docs/latest/submitting-applications.html
  *
  * spark-submit \
  * --name SQLContextDemo \
  * --class com.xiaoxu.spark.SQLContextDemo \
  * --master local[2] \
  * /home/hadoop/lib/sql-1.0.jar \
  * /home/hadoop/app/spark-2.1.0-bin-2.6.0-cdh5.7.0/examples/src/main/resources/people.json
  *
  *
  *
  */
object SQLContextDemo {
  def main(args: Array[String]): Unit = {

    val path = args(0)

    //1.创建相应的context
    val sparkConf = new SparkConf()
    //在测试或者生产中，AppName和Master我们是通过脚本进行指定
    sparkConf.setAppName("SQLContextDemo").setMaster("local[2]");
    val sc = new SparkContext(sparkConf);

    val sqlContext = new SQLContext(sc);
    //2.处理json数据
    val people = sqlContext.read.format("json").load(path);
    people.printSchema()
    people.show()

    //3.关闭资源
    sc.stop()
  }
}
